package parser;

import lexer.TokenType;

/**
 * LogoStatementParser determines which statement type to parse tokens into from
 * the upcoming tokens in TokenIterator
 */
public class LogoStatementParser {
	/**
	 * Creates a new LogoStatement from the iterator, consuming as many tokens
	 * as possible. TokenIterator is incremented to the end of this statement
	 * 
	 * @param TokenIterator
	 *            - the iterator pointing to the start of the statement.
	 * @return One LogoStatement created from the TokenIterator
	 * @throws LogoParseException
	 *             - if an error occurs parsing a statement from the tokens
	 */
	public static LogoStatement parseStatement(TokenIterator it) throws LogoParseException {
		if (!it.hasNext()) {
			throw new LogoParseException("Unexpectedly reached end of program");
		}

		TokenType type = it.peek().getType();
		
		if(type == TokenType.FORWARD || type == TokenType.LEFT || type == TokenType.RIGHT ||
				type == TokenType.SETBLUE || type == TokenType.SETGREEN || type == TokenType.SETRED ||
				type == TokenType.SETSTROKEWIDTH) {
				
			return new LogoCommandStatement(it);
			
		} else if (type == TokenType.IF) {
			return new LogoIfStatement(it);
			
		} else if (type == TokenType.IDENTIFIER) {
			return new LogoProcedureCallStatement(it);
			
		} else {
			throw new LogoParseSyntaxException(it.peek(),
					"FORWARD, LEFT, RIGHT, IF, Function Identifier");
		}
	}
}
